package com.quanyan.place.web;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import com.quanyan.place.entity.apireq.*;
import com.quanyan.place.service.*;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.constants.Constants;
import com.quanyan.place.component.MerchantClientPayType;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.TbBizPlaceMemberCard;
import com.quanyan.place.entity.TbBizPlaceMemberOpHistory;
import com.quanyan.place.entity.apiresp.RespMemberCardType;
import com.quanyan.place.entity.vo.PlaceFinanceAuditVo;
import com.quanyan.place.entity.vo.PlaceMemberTypeVo;
import com.quanyan.place.entity.vo.PlacePrintVo;
import com.quanyan.place.entity.vo.PlaceScanOrderPayVo;
import com.quanyan.place.entity.vo.ReqPlaceMemberSearch;
import com.quanyan.place.wrapper.PlaceMemberWrapper;

/**
 * Created by youxiaojia on 2016/8/23.
 */
@RestController
@RequestMapping("/admin/api/place/member")
public class PlaceMemberController {
	private final static Logger logger = LoggerFactory.getLogger(PlaceMemberController.class);
    @Autowired
    private PlaceMemberService placeMemberService;

    @Autowired
    private PlaceMemberWrapper placeMemberWrapper;
    @Autowired
    private ApiPlaceService apiPlaceService;
    @Autowired
    private PlaceOrderService placeOrderService;
    @Autowired
    PlaceOrderUtilService placeOrderUtilService;
    @Autowired
    FinanceService financeService;
    
    /**
     * 添加会员卡类型
     * @param reqMemberCardType
     * @return
     */
    @RequestMapping(value = "/addMemberType", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> addMemberType(@RequestBody @Valid ReqMemberCardType reqMemberCardType,BindingResult bindingResult)  {
        APIResponse apiResponse = null;
        try {
            apiResponse = placeMemberWrapper.insertMemberType(reqMemberCardType);
        } catch (Exception e) {
            return APIResponse.returnFail(e.getMessage());
        }
        return apiResponse;
    }

    /**
     * 修改会员卡类型
     * @param reqMemberCardType
     * @return
     */
    @RequestMapping(value = "/updateMemberType", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> updateMemberType(@RequestBody @Valid ReqMemberCardType reqMemberCardType,BindingResult bindingResult)  {
        APIResponse apiResponse = placeMemberService.updateMemberType(reqMemberCardType);
        return apiResponse;
    }

    /**
     * 修改会员卡类型(名称，描述)
     * @param map
     * @return
     */
    @RequestMapping(value = "/updateMemberTypeNameAndDescription", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> updateMemberType(@RequestBody Map<String, Object> map,BindingResult bindingResult)  {
        Integer memberCardTypeId =  MapUtils.getInteger(map, "cardTypeId");
        String memberCardTypeName = MapUtils.getString(map, "cardTypeName");
        String memberCardTypeDescription = MapUtils.getString(map, "cardDesc");
        Double discount = MapUtils.getDouble(map,"discount");
        BigDecimal giveAmount =new BigDecimal(0);
        BigDecimal initAmount = new BigDecimal(0);
        if (MapUtils.getDouble(map, "giveAmount") != null){
            giveAmount =new BigDecimal(MapUtils.getDouble(map, "giveAmount"));
        }
        if (MapUtils.getDouble(map, "initAmount") != null){
            initAmount =new BigDecimal(MapUtils.getDouble(map, "initAmount"));
        }
        APIResponse apiResponse = placeMemberService.updateMemberTypeNameAndDescription(memberCardTypeId,memberCardTypeName,
                memberCardTypeDescription, discount, giveAmount, initAmount);
        return apiResponse;
    }

    /**
     * 添加会员及会员卡
     * @param reqMemberCard
     * @return
     */
    @RequestMapping(value = "/addMemberCard", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> addMemberCard(@RequestBody @Valid ReqMemberCard reqMemberCard,BindingResult bindingResult)  {
        boolean isScanPayFlag = false;
        if (null == reqMemberCard.getIsOldMember() || Constants.BYTE_ZERO == reqMemberCard.getIsOldMember()){  //update by tim
            if(MerchantClientPayType.getMatchScanPayType().containsKey(reqMemberCard.getAdminPayType().toString())){
                isScanPayFlag = true;
            }
        }

        boolean isCompanyFlag = false;//对公转账
        if (PlaceConstants.PLACE_ADMIN_PAY_WAYS_COMPANY.equals(reqMemberCard.getAdminPayType().toString())){//对公转账支付方式匹配
            isCompanyFlag = true;
        }

        APIResponse apiResponse = null;
        if (isScanPayFlag) {
            APIResponse mapResponse = null;
            try {
                mapResponse = placeMemberWrapper.addMemberInfo(reqMemberCard,true);
            } catch (Exception e) {
                e.printStackTrace();
                return APIResponse.returnFail("扫码开卡记录生成失败");
            }
            if (mapResponse.isRet() && mapResponse.getData() != null) {
                Map<String, Object> resultMap = (Map<String, Object>) mapResponse.getData();
                TbBizPlaceMemberOpHistory opHistory = (TbBizPlaceMemberOpHistory) resultMap.get("opHistory");
                TbBizPlaceMemberCard tbBizPlaceMemberCard = (TbBizPlaceMemberCard) resultMap.get("memberCard");
                PlaceScanOrderPayVo placeScanOrderPayVo = new PlaceScanOrderPayVo();
                placeScanOrderPayVo.setReceivedAmount(opHistory.getAmount());
                placeScanOrderPayVo.setAdminPayType(reqMemberCard.getAdminPayType().toString());
                placeScanOrderPayVo.setScanCode(reqMemberCard.getScanCode());
                placeScanOrderPayVo.setTbBizPlaceOrder(null);
                placeScanOrderPayVo.setTbBizPlacePrivateOrder(null);
                placeScanOrderPayVo.setReqTicketPay(null);
                placeScanOrderPayVo.setTbBizPlaceMemberCard(tbBizPlaceMemberCard);
                placeScanOrderPayVo.setTbBizPlaceMemberOpHistory(opHistory);
                try {
                    return placeOrderService.scanOrderPay(placeScanOrderPayVo);
                } catch (Exception e) {
                    return APIResponse.returnFail(e.getMessage());
                }
            } else {
                return APIResponse.returnFail("扫码会员开卡失败");
            }
        } else if (isCompanyFlag){
            APIResponse mapResponse = null;
            try {
                mapResponse = placeMemberWrapper.addMemberInfo(reqMemberCard,true);
            } catch (Exception e) {
                e.printStackTrace();
                return APIResponse.returnFail("对公转账开卡记录生成失败");
            }
            if (mapResponse.isRet() && mapResponse.getData() != null) {
                Map<String, Object> resultMap = (Map<String, Object>) mapResponse.getData();
                TbBizPlaceMemberOpHistory opHistory = (TbBizPlaceMemberOpHistory) resultMap.get("opHistory");

                //插入审核记录
                PlaceFinanceAuditVo placeFinanceAuditVo = placeOrderUtilService.tbBizPlaceMemberOpHistory2PlaceFinanceAuditVo(opHistory);
                if (!StringUtils.isEmpty(placeFinanceAuditVo)){
                    financeService.createAuditRecord(placeFinanceAuditVo);
                }

                PlacePrintVo placePrintVo = new PlacePrintVo();
                placePrintVo.setPlaceOrderId(opHistory.getOrderNo());
                placePrintVo.setBusinessType(PlaceConstants.ORDER_USE_OPEN_CARD);
                placePrintVo.setIsMember(PlaceConstants.NO);
                return APIResponse.returnSuccess(placeOrderUtilService.getPrintOrderInfo(placePrintVo));
            } else {
                return APIResponse.returnFail("对公转账会员充值失败");
            }
        } else {
            apiResponse = placeMemberWrapper.addMemberInfo(reqMemberCard,false);
        }
        if (apiResponse.isRet()){
            Map map = (HashMap)apiResponse.getData();
            TbBizPlaceMemberOpHistory tbBizPlaceMemberOpHistory = (TbBizPlaceMemberOpHistory)MapUtils.getObject(map,"opHistory");
            PlacePrintVo placePrintVo = new PlacePrintVo();
            placePrintVo.setPlaceOrderId(tbBizPlaceMemberOpHistory.getOrderNo());
            placePrintVo.setBusinessType(PlaceConstants.ORDER_USE_OPEN_CARD);
            placePrintVo.setIsMember(PlaceConstants.NO);
            return APIResponse.returnSuccess(placeOrderUtilService.getPrintOrderInfo(placePrintVo));
        }
        return apiResponse;
    }


    /**
     * 修改会员卡
     * @param reqMemberCard
     * @return
     */
    @RequestMapping(value = "/updateMemberCard", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> updateMemberCard(@RequestBody @Valid ReqMemberCard reqMemberCard,BindingResult bindingResult)  {
        APIResponse apiResponse = placeMemberWrapper.updateMemberCard(reqMemberCard);
        return apiResponse;
    }
    
    /**
     * 转移会员卡
     * @param reqMemberCard
     * @return
     */
    @RequestMapping(value = "/transferMemberCard", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> transferMemberCard(@RequestBody @Valid ReqMemberCard reqMemberCard,BindingResult bindingResult)  {
    	try {
    		Assert.hasText(reqMemberCard.getInnerCardNo(), "会员卡号不能为空");
    		Assert.hasText(reqMemberCard.getPhone(), "手机号不能为空");
    		Assert.notNull(reqMemberCard.getUid(), "当前操作用户ID不能为空");
    		
    		placeMemberService.transferMemberCard(reqMemberCard.getInnerCardNo(), reqMemberCard.getPhone(), reqMemberCard.getUid());
    		return APIResponse.returnSuccess();
		} catch (Exception e) {
			logger.error("转移会员失败", e);
			return APIResponse.returnFail(e.getMessage());
		}
    }

    /**
     * 调整会员卡余额
     * @param reqAdjustMemberBalance
     * @return
     */
    @RequestMapping(value = "/adjustMemberCardBalance", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> adjustMemberCardBalance(@RequestBody @Valid ReqAdjustMemberBalance reqAdjustMemberBalance,BindingResult bindingResult)  {
        APIResponse apiResponse = placeMemberWrapper.adjustMemberCardBalance(reqAdjustMemberBalance);
        return apiResponse;
    }

    /**
     * 冻结会员
     * @param memberId
     * @return
     */
    @RequestMapping(value = "/logOffMember", method = RequestMethod.POST)
    public APIResponse<?> logOffMember(@RequestParam("memberId") Integer memberId,
                                    @RequestParam("uid") Integer uid){
        APIResponse apiResponse = placeMemberWrapper.frozenMember(memberId, uid);
        return apiResponse;
    }

    /**
     * 根据Id获取指定会员卡信息
     * @param memberCardId
     * @return
     */
    @RequestMapping(value = "/getMemberCardId", method = RequestMethod.GET)
    public APIResponse<?> getMemberCardId(@RequestParam("memberCardId") Integer memberCardId){
        APIResponse apiResponse = placeMemberWrapper.getMemberCardId(memberCardId);
        return apiResponse;
    }

    /**
     * 注销会员卡
     * @param map
     * @return
     */
    @RequestMapping(value = "/logOffMemberCard", method = RequestMethod.POST)
    public APIResponse<?> logOffMemberCard(@RequestBody Map<String, Object> map){
        Integer memberCardId = MapUtils.getInteger(map, "memberCardId");
        Integer uid = MapUtils.getInteger(map, "uid");
        APIResponse apiResponse = placeMemberWrapper.logOffMemberCard(memberCardId, uid);
        return apiResponse;
    }

    /**
     * 修改会员手机号
     * @param reqMemberCard
     * @return
     */
    @RequestMapping(value = "/updatePlaceMemberMobile", method = RequestMethod.POST)
    public APIResponse<?> updatePlaceMemberMobile(@RequestBody ReqMemberCard reqMemberCard){
        APIResponse apiResponse = placeMemberWrapper.updatePlaceMemberMobile(reqMemberCard);
        return apiResponse;
    }
    /**
     * 挂失会员卡
     * @param map
     * @return
     */
    @RequestMapping(value = "/reportLossMemberCard", method = RequestMethod.POST)
    public APIResponse<?> reportLossMemberCard(@RequestBody Map<String, Object> map){
        Integer memberCardId = MapUtils.getInteger(map, "memberCardId");
        Integer uid = MapUtils.getInteger(map, "uid");
        APIResponse apiResponse = placeMemberWrapper.reportLossMemberCard(memberCardId, uid);
        return apiResponse;
    }

    /**
     * 导入场馆会员卡（老会员导入）
     * @return
     */
    @RequestMapping("/importPlaceMembers")
    public APIResponse<?> importPlaceMembers(@RequestBody ReqMemberCards reqMemberCards,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return placeMemberWrapper.importPlaceMembers(reqMemberCards);
    }

    /**
     * 导入场馆会员卡（新会员导入,后续业务处理不同，跟老会员区分开）
     * @return
     */
    @RequestMapping("/importPlaceNewMembers")
    public APIResponse<?> importPlaceNewMembers(@RequestBody ReqMemberCards reqMemberCards,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return placeMemberWrapper.importPlaceMembers(reqMemberCards);
    }


    /**
     * 批量导入场馆会员卡
     * @return
     */
    @RequestMapping("/importPlaceMembersBatch")
    public APIResponse<?> importPlaceMembersBatch(@RequestBody ReqMemberCards reqMemberCards,BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return placeMemberWrapper.importPlaceMembersBatch(reqMemberCards);
    }

    /**
     * 获取指定场馆会员类型
     * @param placeId
     * @return
     */
    @RequestMapping(value = "/getMemberTypeByPlaceId", method = RequestMethod.POST)
    public APIResponse<?> getMemberTypeByPlaceId(@RequestParam("placeId") Integer placeId){
        APIResponse apiResponse = placeMemberWrapper.getMemberTypeByPlaceId(placeId);
        return apiResponse;
    }

    /**
     * 获取会员卡列表
     * @param reqMemberSearch
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/getPlaceMemberCardList", method = RequestMethod.POST)
    public APIResponse<?> getPlaceMemberCardList(@RequestBody @Valid ReqPlaceMemberSearch reqMemberSearch, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }

        return APIResponse.returnSuccess(placeMemberWrapper.getPlaceMemberCardList(reqMemberSearch));
    }


    /**
     * 分页查询会员类型列表
     * @param placeMemberTypeVo
     * @return
     */
    @RequestMapping(value = "/queryMemberTypeList", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> queryMemberTypeList(@RequestBody PlaceMemberTypeVo placeMemberTypeVo,BindingResult bindingResult)  {
        APIResponse apiResponse = placeMemberWrapper.queryMemberTypeListByConditions(placeMemberTypeVo);
        return apiResponse;
    }

    /**
     * 会员类型列表导出
     * @param placeMemberTypeVo
     * @return
     */
    @RequestMapping(value = "/queryMemberTypeListExport", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> queryMemberTypeListExport(@RequestBody PlaceMemberTypeVo placeMemberTypeVo,BindingResult bindingResult)  {
        List<RespMemberCardType> list = placeMemberService.queryMemberTypeListExport(placeMemberTypeVo);
        return APIResponse.returnSuccess(list);
    }

    /**
     * 获取会员卡类型字典
     * @return
     */
    @RequestMapping(value = "/getMemberTypeDict", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getMemberTypeDict()  {
        return placeMemberService.getMemberTypeDictDisplayName();
    }

    /**
     * 获取会员类型详情
     * @return
     */
    @RequestMapping(value = "/getMemberTypeDetail", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getMemberTypeDetail(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer id = MapUtils.getInteger(map, "id");
        if (!StringUtils.isEmpty(id)){
            return placeMemberService.getMemberTypeDetailById(id);
        }else{
            return APIResponse.returnFail("id is null");
        }
    }

    /**
     * 根据删除会员类型
     * @return
     */
    @RequestMapping(value = "/delMemberType", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> delMemberTypeById(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer id = MapUtils.getInteger(map, "id");
        if (!StringUtils.isEmpty(id)){
            return placeMemberService.delMemberTypeById(id);
        }else{
            return APIResponse.returnFail("id is null");
        }
    }

    /**
     * 根据id修改会员类型状态
     * @return
     */
    @RequestMapping(value = "/updateMemberTypeStatus", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> updateMemberTypeStatus(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer id = MapUtils.getInteger(map, "id");
        byte status = MapUtils.getByte(map, "status");
        if (StringUtils.isEmpty(id)){
            return APIResponse.returnFail("id is null");

        }
        if (StringUtils.isEmpty(status)) {
            return APIResponse.returnFail("status is null");
        }
        return placeMemberWrapper.updateMemberTypeStatusById(id,status);
    }

    /**
     * 获取会员卡操作明细
     * @param reqMemberCardOperate
     * @return
     */
    @RequestMapping(value = "/queryMemberCardOperateDetailList", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> queryMemberCardOperateDetailList(@RequestBody @Valid ReqMemberCardOperate reqMemberCardOperate, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        APIResponse apiResponse = placeMemberService.getMemberCardOperateDetail(reqMemberCardOperate);
        return apiResponse;
    }

    /**
     * 会员卡记录导出
     * @param reqMemberCardOperate
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/queryMemberCardOperateDetailExport", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> queryMemberCardOperateDetailExport(@RequestBody @Valid ReqMemberCardOperate reqMemberCardOperate, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        return APIResponse.returnSuccess(placeMemberService.getMemberCardOperateDetailExport(reqMemberCardOperate));
    }
    /**
     * 根据删除会员类型
     * @return
     */
    @RequestMapping(value = "/getMemberCardOperateDetail", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getMemberCardOperateDetail(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer id = MapUtils.getInteger(map, "id");
        if (!StringUtils.isEmpty(id)){
            return placeMemberService.getMemberCardOperateDetailById(id);
        }else{
            return APIResponse.returnFail("id is null");
        }
    }

    /**
     * 获取会员类型详情
     * @return
     */
    @RequestMapping(value = "/getMemberTypeNameByPlaceId", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getMemberTypeNameByPlaceId(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer id = MapUtils.getInteger(map, "placeId");
        if (!StringUtils.isEmpty(id)){
            return placeMemberService.getMemberTypeNameByPlaceId(id);
        }else{
            return APIResponse.returnFail("placeId is null");
        }
    }

    /**
     * 获取会员类型详情
     * @return
     */
    @RequestMapping(value = "/getMemberStatisticsByPlaceId", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getMemberStatisticsByPlaceId(@RequestBody @Valid ReqMemberCardOperate req, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer id = req.getPlaceId();
        if (!StringUtils.isEmpty(id)){
            return placeMemberService.getMemberStatisticsByPlaceId(req);
        }else{
            return APIResponse.returnFail("placeId is null");
        }
       // return placeMemberService.getMemberStatisticsByPlaceId(req);
    }

    /**
     * 获取会员类型详情
     * @return
     */
    @RequestMapping(value = "/getMemberTotalDataByPlaceId", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> getMemberTotalDataByPlaceId(@RequestBody @Valid ReqMemberCardOperate req, BindingResult bindingResult)  {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer id = req.getPlaceId();
        if (!StringUtils.isEmpty(id)){
            return placeMemberService.getMemberTotalDataByPlaceId(req);
        }else{
            return APIResponse.returnFail("placeId is null");
        }
        //return placeMemberService.getMemberTotalDataByPlaceId(req);
    }

    /**
     * 获取会员类型详情
     * @return
     */
    @RequestMapping(value = "/exportMemberStatisticsByPlaceId", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> exportMemberStatisticsByPlaceId(@RequestBody @Valid ReqMemberCardOperate req, BindingResult bindingResult)  {
       /* ReqMemberCardOperate reqMemberCardOperate =new ReqMemberCardOperate();
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer id = MapUtils.getInteger(map, "placeId");
        if (!StringUtils.isEmpty(id)){
            return placeMemberService.getMemberStatisticsByPlaceId(reqMemberCardOperate);
        }else{
            return APIResponse.returnFail("placeId is null");
        }*/
        return placeMemberService.exportMemberStatisticsByPlaceId(req);
    }

    /**
     * 根据手机号或电话模糊匹配会员信息
     */
    @RequestMapping(value = "/queryPlaceMemberByPhoneOrName", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> queryPlaceMemberByPhoneOrName(@RequestBody @Valid Map<String, Object> map, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID);
        }
        Integer placeId = MapUtils.getInteger(map, "placeId");
        String phoneOrName = MapUtils.getString(map, "phoneOrName");
        if (StringUtils.isEmpty(placeId)){
            return APIResponse.returnFail("场馆id为空！");
        }
        if (!StringUtils.isEmpty(phoneOrName)) {
            return placeMemberService.queryPlaceMemberByPhoneOrName(placeId, phoneOrName);
        } else {
            return APIResponse.returnFail("请输入会员名称或电话！");
        }
    }


    /**
     * 显示商家端会员卡充值
     * @param map
     * @return
     */
    @RequestMapping(value = "/showCardRecharge", method = RequestMethod.POST)
    @ResponseBody
    public APIResponse<?> showCardRecharge(@RequestBody  @Valid Map<String, Object> map){
        Integer cardID = MapUtils.getInteger(map, "cardID");
        if (StringUtils.isEmpty(cardID)){
            return APIResponse.returnFail("cardID不能为空");
        }
        Integer placeId = MapUtils.getInteger(map,"placeId");
        return placeMemberService.showCardRecharge(cardID,placeId);
    }

    /**
     * 商家端会员卡充值
     * @param map
     * @return
     */
    @RequestMapping(value = "/cardRecharge")
    @ResponseBody
    public APIResponse<?> cardRecharge(@RequestBody  @Valid Map<String, Object> map){
        Integer cardID = MapUtils.getInteger(map, "cardID");
        Integer createUid = MapUtils.getInteger(map, "createUid");
        Integer adminPayType = MapUtils.getInteger(map, "adminPayType");
        Double dAmount = MapUtils.getDouble(map, "amount", 0d);
        Double dGiveAmout = MapUtils.getDouble(map, "giveAmount", 0d);
        Integer numbers = MapUtils.getInteger(map, "numbers");
        String scanCode = MapUtils.getString(map,"scanCode");
        String memo = MapUtils.getString(map, "memo");

        if (dAmount <= 0){
            return APIResponse.returnFail("充值金额不正确");
        }
        if (dGiveAmout < 0){
            return APIResponse.returnFail("赠送金额不正确");
        }
        if (numbers != null && numbers <= 0){
            return APIResponse.returnFail("充值次数不正确");
        }
        if (memo.length() > 150){
            return APIResponse.returnFail("备注超出最大范围");
        }

        if (StringUtils.isEmpty(cardID)){
            return APIResponse.returnFail("会员卡充值失败");
        }

        return placeMemberWrapper.cardRecharge(cardID, createUid, adminPayType, dAmount, dGiveAmout,
                numbers,scanCode, memo);
    }

    /**
     * 设置场馆为通馆（可共享会员）
     * @return
     */
    @RequestMapping(value = "/setPlaceShareMember")
    public APIResponse<?> setPlaceShareMember(@RequestBody Map<String, Object> map){
        Integer placeId = MapUtils.getInteger(map, "placeId");
        Integer relatePlaceId = MapUtils.getInteger(map, "relatePlaceId");
        if (null == placeId){
            return APIResponse.returnFail("场馆id不能为空");
        }
        if (null == relatePlaceId){
            return APIResponse.returnFail("关联场馆id不能为空");
        }
        APIResponse result = placeMemberWrapper.setPlaceShareMember(placeId, relatePlaceId);
        return result;
    }

    /**
     * 根据placeId获取其它互通会员的场馆
     * @return
     */
    @RequestMapping(value = "/getSharePlace", method = RequestMethod.POST)
    public APIResponse<?> getSharePlace(@RequestBody Map<String, Object> map){
        Integer placeId = MapUtils.getInteger(map, "placeId");
        APIResponse apiResponse = placeMemberWrapper.getSharePlace(placeId);
        return apiResponse;
    }

    /**
     * 解除场馆关联会员关系
     * @param map
     * @return
     */
    @RequestMapping(value = "/cancelPlaceShareMember")
    public APIResponse<?> cancelPlaceShareMember(@RequestBody Map<String, Object> map){
        Integer placeId = MapUtils.getInteger(map, "placeId");
        Integer relatePlaceId = MapUtils.getInteger(map, "relatePlaceId");
        if (StringUtils.isEmpty(placeId)){
            return APIResponse.returnFail("场馆Id不能为空");
        }
        if (StringUtils.isEmpty(relatePlaceId)){
            return APIResponse.returnFail("关联场馆id不能为空");
        }
        APIResponse result = placeMemberWrapper.cancelPlaceShareMember(relatePlaceId);

        return result;
    }

    /**
     *  批量将线上场馆支持会员卡场馆导入到redis
     * @param
     * @return
     */
    @RequestMapping(value = "/addSupportMemberToRedis",method = RequestMethod.POST)
    public APIResponse<?> addSupportMemberToRedis(@RequestBody Map<String, Object> map) {
        return placeMemberService.updateSupportMemberPlaceBatch();
    }

    @Autowired
    private AppBannerService appBannerService;

    /**
     * 插入馆长信息
     * @param reqPlaceLeaderMsg
     * @return
     */
    @RequestMapping(value = "/addPlaceLeaderMessage",method = RequestMethod.POST)
    public APIResponse<?> addPlaceLeaderMessage(@RequestBody @Valid ReqPlaceLeaderMsg reqPlaceLeaderMsg) {
        return appBannerService.insertPlaceLeaderMsg(reqPlaceLeaderMsg);
    }

    /**
     * 编辑馆长信息
     * @param reqPlaceLeaderMsg
     * @return
     */
    @RequestMapping(value = "/editPlaceLeaderMessage",method = RequestMethod.POST)
    public APIResponse<?> editPlaceLeaderMessage(@RequestBody @Valid ReqPlaceLeaderMsg reqPlaceLeaderMsg) {
        return appBannerService.editPlaceLeaderMsg(reqPlaceLeaderMsg);
    }

    /**
     * 获取馆长信息
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/getPlaceLeaderMessage",method = RequestMethod.POST)
    public APIResponse<?> getPlaceLeaderMessage(@RequestBody Map<String,Object> paramMap) {
        if (paramMap.containsKey("placeId")) {
            return appBannerService.getPlaceLeaderMsg((Integer) paramMap.get("placeId"));
        }
        return APIResponse.returnFail("场馆编号不能为空");
    }
}
